package com.leyou.lyitem.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.lyitem.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("list")
    public ResponseEntity<PageResult<Brand>> findBrand(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",defaultValue = "false")boolean desc,
            @RequestParam(value = "key",required = false)String key
    ){
        PageResult<Brand> pageResult = this.brandService.findBrand(page,rows,sortBy,desc,key);

        System.out.println(pageResult.getItems());
        if(CollectionUtils.isEmpty(pageResult.getItems())){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("add")
    public ResponseEntity<Void> addBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        this.brandService.addBrand(brand,cids);

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping("edit")
    public ResponseEntity<Void> editBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        this.brandService.editBrand(brand,cids);

        return new ResponseEntity<>(HttpStatus.OK);
    }
    @RequestMapping("delete")
    public ResponseEntity<Void> deleteBrand(@RequestParam("bid")Long bid){
        this.brandService.deleteBrand(bid);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
